package org.example.module.api.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.common.BaseWarehouseReqParam;
import org.example.common.BaseWarehouseSysTypeEnum;
import org.example.common.base.request.CommonRequestParam;
import org.example.common.base.response.Result;
import org.example.module.api.model.outbound.CreateOutboundReqParam;
import org.example.module.api.model.outbound.CreateOutboundResParam;
import org.example.module.api.service.WarehouseService;
import org.example.wh.cope.param.CopeReqParam;
import org.example.wh.cope.param.CopeResParam;
import org.example.wh.cope.param.createOutbound.CopeCreateOutboundReqParam;
import org.example.wh.cope.param.createOutbound.CopeCreateOutboundResParam;
import org.example.wh.cope.util.CopeHttpUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

@Slf4j
@Service
public class CopeWarehouseServiceImpl implements WarehouseService {

    @Override
    public String getCode() {
        return BaseWarehouseSysTypeEnum.COPE.getCode();
    }

    @Override
    public Result<CreateOutboundResParam> createOutbound(CommonRequestParam<CreateOutboundReqParam> request) {
        BaseWarehouseReqParam<CopeReqParam<CopeCreateOutboundReqParam>> param = prepareRequest(request,
                body -> {
                    CreateOutboundReqParam createOutboundReqParam = (CreateOutboundReqParam) body;

                    CopeCreateOutboundReqParam copeCreateOutboundReqParam = CopeCreateOutboundReqParam.builder()
                            .sourceNo(createOutboundReqParam.getOrderNo())
                            .warehouse(createOutboundReqParam.getWarehouseCode())
                            .build();

                    return copeCreateOutboundReqParam;
                });

        CopeResParam<CopeCreateOutboundResParam> result = CopeHttpUtil.createOutbound(param);

        CreateOutboundResParam resParam = null;
        if (Objects.nonNull(result) && result.getCode() == 200) {
            if (Objects.nonNull(result.getData())) {
                CopeCreateOutboundResParam source = result.getData();

                resParam = CreateOutboundResParam.builder()
                        .orderNo(String.valueOf(source.getOrderId()))
                        .build();
            }
            return Result.success(resParam);
        }
        return Result.error(Result.FAIL_CODE, result.getMessage());
    }

    /**
     * 准备请求对象
     */
    private <T> BaseWarehouseReqParam<CopeReqParam<T>> prepareRequest(CommonRequestParam<?> param, Function<Object, T> converter) {
        if (StringUtils.isEmpty(param.getCode())) {
            throw new RuntimeException("仓库编码为空！");
        }

        Map<String, String> authInfo = param.getAuthInfo();
        if (CollectionUtil.isEmpty(authInfo)) {
            throw new RuntimeException("授权信息为空！");
        }

        T data = converter.apply(param.getBody());
        CopeReqParam<T> theCopeReqParam = new CopeReqParam<>();
        theCopeReqParam.setXApiKey(authInfo.get("x-api-key"));
        theCopeReqParam.setData(data);

        BaseWarehouseReqParam<CopeReqParam<T>> request = new BaseWarehouseReqParam<>();
        request.setSysUrl(param.getSysUrl());
        request.setAuthInfo(param.getAuthInfo());
        request.setBody(theCopeReqParam);
        return request;
    }
}
